﻿using System;
using System.Linq;
using Solution.BusinessObject;
using Solution.Controls;
using Solution.Core;

namespace Solution
{
    public partial class Service : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // get language
            var language = new LanguageBO().GetLanguage(this.Language);

            // get data
            var category = CommonBO.MasterEDM.Categories
                .FirstOrDefault(t => t.Name == "Service"
                                     && t.Language.Id == language.Id);

            if (category == null)
            {
                return;
            }

            // set page title
            Page.Title = category.Title;

            Content content;
            if (Request["id"] != null)
            {
                var contentId = Request["id"];
                content = new ContentBO().GetContent(category,
                                                     int.Parse(contentId));
            }
            else
            {
                content = new ContentBO().GetLastContent(category);
            }

            // bind to page
            if (content == null)
            {
                return;
            }
            this.lblTitle.Text = content.Title;
            this.lblIntro.Text = content.Intro;
            this.lblDetail.Text = content.Detail;

            // Load related content
            var relatedContent = (RelatedContent)this.LoadControl("Controls/RelatedContent.ascx");
            relatedContent.Category = category;
            relatedContent.ContentRelated = content;

            this.phdRelatedContent.Controls.Add(relatedContent);
        }
    }
}